package com.ruoyi.elasticsearch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.elasticsearch.domain.AlarmEntity;
import com.ruoyi.elasticsearch.es.domain.DeviceDTO;
import com.ruoyi.elasticsearch.es.domain.DeviceInfoDTO;
import com.ruoyi.elasticsearch.es.domain.QuotaDTO;
import com.ruoyi.elasticsearch.mapper.AlarmMapper;
import com.ruoyi.elasticsearch.service.AlarmService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, AlarmEntity> implements AlarmService {


    @Override
    public AlarmEntity verifyQuota(QuotaDTO quotaDTO) {
        //1.根据指标id查询告警判断规则列表
        List<AlarmEntity> alarmEntityList = getByQuotaId(quotaDTO.getId());
        AlarmEntity alarm=null;
        for( AlarmEntity alarmEntity:alarmEntityList ){
            //判断：操作符和指标对比
            if( "String".equals( quotaDTO.getValueType() ) || "Boolean".equals(quotaDTO.getValueType())  ){
                if(  alarmEntity.getOperator().equals("=")  &&  quotaDTO.getStringValue().equals(alarmEntity.getThreshold()) ){
                    alarm=alarmEntity;
                    break;
                }
            }else //数值
            {
                if(  alarmEntity.getOperator().equals(">")  &&  quotaDTO.getValue()>alarmEntity.getThreshold() ){
                    alarm=alarmEntity;
                    break;
                }
                if(  alarmEntity.getOperator().equals("<")  &&  quotaDTO.getValue()<alarmEntity.getThreshold() ){
                    alarm=alarmEntity;
                    break;
                }
                if(  alarmEntity.getOperator().equals("=")  &&  quotaDTO.getValue().equals(alarmEntity.getThreshold()) ){
                    alarm=alarmEntity;
                    break;
                }
            }
        }

        return alarm;
    }

    @Override
    public List<AlarmEntity> getByQuotaId(Integer quotaId) {
        QueryWrapper<AlarmEntity> wrapper = new QueryWrapper<>();
        wrapper
                .lambda()
                .eq(AlarmEntity::getQuotaId,quotaId)
                .orderByDesc(AlarmEntity::getLevel);

        return this.list(wrapper);
    }

    @Override
    public DeviceInfoDTO verifyDeviceInfo(DeviceInfoDTO deviceInfoDTO) {
        // 封装指标的告警  封装设备的告警
        DeviceDTO deviceDTO = deviceInfoDTO.getDevice();

        deviceDTO.setLevel(0);//假设不告警
        deviceDTO.setAlarm(false);
        deviceDTO.setAlarmName("正常");
        deviceDTO.setStatus(true);
        deviceDTO.setOnline(true);

        for(QuotaDTO quotaDTO :deviceInfoDTO.getQuotaList() ){

            AlarmEntity alarmEntity = verifyQuota(quotaDTO);//根据指标得到告警信息
            if(alarmEntity!=null){  //如果指标存在告警

                quotaDTO.setAlarm("1");
                quotaDTO.setAlarmName( alarmEntity.getName() );//告警名称
                quotaDTO.setLevel( alarmEntity.getLevel()+"" );//告警级别
                quotaDTO.setAlarmWebHook(alarmEntity.getWebHook());//告警web钩子
                quotaDTO.setCycle( alarmEntity.getCycle() );//沉默周期

                if(alarmEntity.getLevel().intValue()> deviceDTO.getLevel().intValue() ){

                    deviceDTO.setLevel( alarmEntity.getLevel() );
                    deviceDTO.setAlarm(true);
                    deviceDTO.setAlarmName(alarmEntity.getName());
                }

            }else{//如果指标不存储在告警
                quotaDTO.setAlarm("0");
                quotaDTO.setAlarmName("正常");
                quotaDTO.setLevel("0");
                quotaDTO.setAlarmWebHook("");
                quotaDTO.setCycle(0);
            }
        }
        return deviceInfoDTO;
    }
}
